//
//  ZZMyHeaderView.m
//  ZuZuBao
//
//  Created by Felix on 2024/4/9.
//

#import "ZZMyHeaderView.h"
#import "ZZItemButton.h"

@interface ZZMyHeaderView()

@property (nonatomic, strong) UIImageView * backImgView;
@property (nonatomic, strong) UIImageView * headImgView;
@property (nonatomic, strong) UILabel * nameLabel;
@property (nonatomic, strong) UILabel * phoneLabel;
@property (nonatomic, strong) ZZIconButton * arrowButton;

@property (nonatomic, strong) UILabel * weiLabel;
@property (nonatomic, strong) UIButton * lgButton;

@end

@implementation ZZMyHeaderView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 171)];
        self.backImgView.image = [UIImage imageNamed:@"my_back_img"];
        [self addSubview:self.backImgView];
        
        self.headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(18, 171 - 25 - 66, 66, 66)];
        self.headImgView.layer.cornerRadius = 33;
        self.headImgView.layer.masksToBounds = YES;
        [self addSubview:self.headImgView];
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headImgView.frame) + 14, self.headImgView.frame.origin.y + 12, 200, 22)];
        self.nameLabel.textColor = k333Color;
        self.nameLabel.font = [UIFont systemFontOfSize:20];
        self.nameLabel.hidden = YES;
        [self addSubview:self.nameLabel];
        
        self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, CGRectGetMaxY(self.nameLabel.frame) + 5, 100, 18)];
        self.phoneLabel.textColor = k999Color;
        self.phoneLabel.font = [UIFont systemFontOfSize:12];
        self.phoneLabel.hidden = YES;
        [self addSubview:self.phoneLabel];
        
        self.weiLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headImgView.frame) + 14, self.headImgView.frame.origin.y, 80, 66)];
        self.weiLabel.textColor = k666Color;
        self.weiLabel.font = [UIFont systemFontOfSize:20];
        self.weiLabel.text = @"未登录";
        [self addSubview:self.weiLabel];
        
        self.arrowButton = [[ZZIconButton alloc] initWithFrame:CGRectMake(kScreenWidth - 8 - 40, self.headImgView.center.y - 20, 40, 40)];
        [self.arrowButton setButtonImage:@"arrow_right_icon"];
        [self.arrowButton addTarget:self action:@selector(detailAction) forControlEvents:UIControlEventTouchUpInside];
        self.arrowButton.hidden = YES;
        [self addSubview:self.arrowButton];
        
        self.lgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.lgButton.frame = CGRectMake(kScreenWidth - kPadding - 76, self.headImgView.center.y - 18, 76, 36);
        self.lgButton.backgroundColor = [UIColor blackColor];
        self.lgButton.layer.cornerRadius = 18;
        self.lgButton.layer.masksToBounds = YES;
        [self.lgButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.lgButton setTitle:@"登录" forState:UIControlStateNormal];
        self.lgButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [self.lgButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.lgButton];
        
        UIView * wView = [[UIView alloc] initWithFrame:CGRectMake(kPadding, self.backImgView.frame.origin.y + self.backImgView.frame.size.height, kScreenWidth - kPadding * 2, 123)];
        wView.backgroundColor = [UIColor whiteColor];
        wView.layer.cornerRadius = 12;
        wView.layer.masksToBounds = YES;
        [self addSubview:wView];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(kPadding, 10, 100, 28)];
        label.textColor = k333Color;
        label.font = [UIFont boldSystemFontOfSize:16];
        label.text = @"我的订单";
        [wView addSubview:label];
        
        CGFloat width = wView.frame.size.width / 4;
        NSArray * array = @[@"待收货", @"租用中", @"待归还", @"已完成"];
        NSArray * array2 = @[@"icon_ready_received", @"icon_rent_ing", @"icon_to_back", @"icon_have_finished"];
        for (int i = 0; i < array.count; i++) {
            ZZItemButton * button = [[ZZItemButton alloc] initWithFrame:CGRectMake(width * i, 52, width, 61)];
            [button setButtonImage:array2[i] text:array[i]];
            button.tag = 2000 + i;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [wView addSubview:button];
        }
    }
    return self;
}

-(void)setViewData {
    if (kUid.length > 0) {
        self.weiLabel.hidden = YES;
        self.lgButton.hidden = YES;
        self.nameLabel.hidden = NO;
        self.phoneLabel.hidden = NO;
        self.arrowButton.hidden = NO;
        
        NSLog(@"kHeadImg~~~~~~%@", kHeadImg);
        NSLog(@"kNickName~~~~~~%@", kNickName);
        [self.headImgView sd_setImageWithURL:[NSURL URLWithString:kHeadImg]];
        self.nameLabel.text = kNickName;
        if (kPhone.length > 7) {
            self.phoneLabel.text = [kPhone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        } else {
            self.phoneLabel.text = kPhone;
        }
    } else {
        self.weiLabel.hidden = NO;
        self.lgButton.hidden = NO;
        self.nameLabel.hidden = YES;
        self.phoneLabel.hidden = YES;
        self.arrowButton.hidden = YES;
        self.headImgView.backgroundColor = kBackColor;
    }
}

-(void)detailAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickToMyInfo)]) {
        [self.delegate clickToMyInfo];
    }
}

-(void)buttonAction:(ZZIconButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickToItem:)]) {
        [self.delegate clickToItem:button.tag - 2000];
    }
}

- (void)loginAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickToLogin)]) {
        [self.delegate clickToLogin];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
